package com.xci.platform.auth.controller;

import com.xci.platform.annotation.Authorize;
import com.xci.platform.auth.entity.DicEntity;
import com.xci.platform.auth.service.DicService;
import com.xci.platform.core.BoolMessage;
import com.xci.platform.core.Constant;
import com.xci.platform.core.TreeNode;
import com.xci.platform.core.TreeNodeIndex;
import com.xci.platform.helper.ExcelHelper;
import com.xci.platform.helper.StringHelper;
import com.xci.platform.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 系统字典控制器
 */
@Authorize
@Slf4j
@Controller
@RequestMapping("/auth/dic")
public class DicController extends BaseController {

    /**
     * 字典服务
     */
    @Resource
    private DicService dicService;

    //region 页面视图

    /**
     * 字典首页
     */
    @GetMapping
    @Authorize(code = "")
    public String index() {
        return "auth/dic/index";
    }


    /**
     * 字典新建页面
     */
    @GetMapping("/create")
    @Authorize(code = "")
    public String create(String parentId, ModelMap map) {
        createMark(map);
        DicEntity entity = new DicEntity();
        entity.setId(StringHelper.guid());
        entity.setParentId(parentId);
        map.put("entity", entity);
        map.put("parentName", getParentName(parentId));
        return "auth/dic/edit";
    }

    /**
     * 字典编辑页面
     */
    @GetMapping("/edit")
    @Authorize(code = "")
    public String edit(String id, ModelMap map) {
        DicEntity entity = dicService.queryById(id);
        if (entity == null) return notFound();
        map.put("entity", entity);
        map.put("parentName", getParentName(entity.getParentId()));
        return "auth/dic/edit";
    }

    private String getParentName(String id) {
        String parentName = Constant.Empty;
        if (StringHelper.isNotBlank(id) && !id.equals(Constant.RootNodeId)) {
            parentName = dicService.queryNameById(id);
        }
        if (StringHelper.isBlank(parentName)){
            parentName = "顶层字典";
        }
        return parentName;
    }

    /**
     * 字典详细页面
     */
    @GetMapping("/details")
    @Authorize(code = "")
    public String details(String id, ModelMap map) {
        DicEntity entity = dicService.queryById(id);
        if (entity == null) return notFound();
        map.put("entity", entity);
        return "auth/dic/details";
    }

    //endregion

    //region 数据处理

    /**
     * 字典Tree
     */
    @ResponseBody
    @GetMapping("/tree")
    public Object tree() {
        List<DicEntity> list = dicService.query();
        List<TreeNode> models = dicService.convertToNodeList(list);
        return BoolMessage.tree(models);
    }

    /**
     * 字典表格数据
     */
    @ResponseBody
    @GetMapping("/grid")
    @Authorize(code = "")
    public Object grid() {
        List<DicEntity> list = dicService.query();
        return BoolMessage.grid(list);
    }

    /**
     * 保存新增字典
     */
    @ResponseBody
    @PostMapping("/create-save")
    @Authorize(code = "")
    public BoolMessage createSave(@ModelAttribute DicEntity entity ) {
        return dicService.save(entity, true);
    }

    /**
     * 保存修改字典
     */
    @ResponseBody
    @PostMapping("/edit-save")
    @Authorize(code = "")
    public BoolMessage editSave(@ModelAttribute DicEntity entity ) {
        return dicService.save(entity, false);
    }

    /**
     * 保存拖拽数据(父节点和路径)
     */
    @ResponseBody
    @PostMapping("/dnd")
    @Authorize(code = "")
    public BoolMessage dnd(@RequestBody TreeNodeIndex[] nodeIndexs) {
        return dicService.updateNodeIndex(nodeIndexs);
    }

    /**
     * 删除字典
     */
    @ResponseBody
    @PostMapping("/delete")
    @Authorize(code = "")
    public BoolMessage delete(String id) {
        return dicService.delete(id);
    }

    /**
     * 导出字典
     */
    @GetMapping("/export")
    @Authorize(code = "")
    public void export(@RequestParam Map params, HttpServletResponse response) throws IOException {
        ExcelHelper.exportWeb(response, dicService.query(), DicEntity.class, "系统字典");
    }

    //endregion
}
